<?php
namespace app\index\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Request;

use service\FeishuService;
use service\GuardService;

class CronFeishucontact extends Command
{
    /**
     * 基础配置信息
     */
    protected function configure()
    {
        parent::configure();

        $this->setName('cronfeishucontact')
            ->setDescription('更新飞书通讯录部门或员工')
            ->setHelp('php think cronfeishucontact 更新类型（1部门  2员工）；')
            ->addArgument('utype');
    }

    /**
     * 执行体
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     */
    protected function execute(Input $input, Output $output)
    {
        $utype = $input->getArgument('utype');
        if (empty($utype)) {
            $utype = 1;
        }
        if (!empty($utype) && !in_array($utype, [1, 2])) {
            exit("参数错误！1代表部门更新，2代表员工更新" . PHP_EOL);
        }
        //echo $utype.PHP_EOL;exit;
        $starttime = explode(' ',microtime());
        echo "start".PHP_EOL;
        $ctime = time();
        if($utype == 1){
            //更新集团总部
            $main_department_lists = FeishuService::instance()->GetDeparmentlists();
            foreach ($main_department_lists as $main_department_list){
                $main_dep_data = [];
                $main_dep_data['department_id'] = $main_department_list['department_id'];
                $main_dep_data['title'] = $main_department_list['name'];
                if(isset($main_department_list['leader_user_id']) && !empty($main_department_list['leader_user_id'])){
                    $main_dep_data['leader_user_id'] = $main_department_list['leader_user_id'];
                }
                if(isset($main_department_list['leaders']) && !empty($main_department_list['leaders'])){
                    $main_dep_data['leaders'] = json_encode($main_department_list['leaders'],304);
                }
                $main_dep_data['member_count'] = $main_department_list['member_count'];
                $main_dep_data['primary_member_count'] = $main_department_list['primary_member_count'];
                $main_dep_data['open_department_id'] = $main_department_list['open_department_id'];
                $main_dep_data['pid'] = $main_dep_data['parent_department_id'] = $main_department_list['parent_department_id'];
                $main_dep_data['sort'] = $main_department_list['order'];
                $main_dep_data['status'] = isset($main_department_list['status']['is_deleted']) && $main_department_list['status']['is_deleted'] ? -1 : 1;
                $main_dep_data['is_jtzb'] = 1;
                $res = GuardService::instance()->AddDepartment($main_dep_data);
                if($res['code'] == 0 && $res['message'] == 'success'){
                    $main_dep_data['update_guard_state'] = 1;
                }
                else{
                    $main_dep_data['update_guard_state'] = 0;
                }
                $main_dep_data['update_guard_res'] = json_encode($res,304);
                $departinfo = Db::name("department")->where(['department_id'=>$main_department_list['department_id']])->find();
                if(!empty($departinfo)){
                    $main_dep_data['update_time'] = $ctime;
                    Db::name("department")->where(['id'=>$departinfo['id']])->update($main_dep_data);
                }
                else{
                    $main_dep_data['create_time'] = $main_dep_data['update_time'] = $ctime;
                    Db::name("department")->insert($main_dep_data);
                }
                FeishuService::instance()->GetchildrendepByfdepid($main_department_list['department_id'],$ctime);
            }
            Db::name("department")->where(['update_time'=>['<>',$ctime]])->update(['update_state'=>0,'is_jtzb'=>0]);
        }
        else{
            $departmentlists = Db::name("department")->where(['status'=>1])->where(['is_jtzb'=>1])->order("id asc")->select();
            foreach ($departmentlists as $departmentlist){
                FeishuService::instance()->GetuserBydeparmentid($departmentlist['department_id'],$ctime);
            }
            Db::name("user")->where(['update_time'=>['<>',$ctime]])->update(['update_state'=>0,'is_jtzb'=>0]);
        }
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本次执行耗时：".$thistime." 秒。".PHP_EOL;
    }

}